import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getAllarticle, getAllTag, getAllSortTag } from "@/services";
import {IAll} from '@/utils/interface'
export interface IndexModelState {
    name: string,
    articles_allList1:IAll[],
    articles_allList2:IAll[],
    articles_allList3:IAll[]
}

export interface IndexModelType {
    namespace: 'articles_all';
    state: IndexModelState;
    effects: {
        query: Effect;
        querys: Effect;
        sort: Effect;
    };
    reducers: {
        save: Reducer<IndexModelState>;
        // 启用 immer 之后
        // save: ImmerReducer<IndexModelState>;
    };
    subscriptions?: { setup: Subscription };
}

const IndexModel: IndexModelType = {
    namespace: 'articles_all',

    state: {
        name: '',
        articles_allList1:[],
        articles_allList2:[],
        articles_allList3:[]
    },

    effects: {
        *query({ payload }, { call, put }) {
            // 所有文章
            let res = yield call(getAllarticle)
            console.log(res.data[0], "所有文章");
            yield put({
                type: "save",
                payload: {
                    articles_allList1: res.data[0]
                }
            });
        },

        *querys({ payload }, { call, put }) {
            // 标签管理
            let res = yield call(getAllTag)
            console.log(res, "标签管理...");
            yield put({
                type: "save",
                payload: {
                    articles_allList2: res
                }
            });
        },

        *sort({ payload }, { call, put }) {
            // 标签管理
            let res = yield call(getAllSortTag)
            console.log(res, "标签分类...");
            yield put({
                type: "save",
                payload: {
                    articles_allLis3: res
                }
            });
        }
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
        // 启用 immer 之后
        // save(state, action) {
        //   state.name = action.payload;
        // },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathname === '/') {
                    dispatch({
                        type: 'query',
                    });
                }
            });
        },
    },
};

export default IndexModel;